package com.simon.system.controller;

import java.util.List;

import com.simon.exception.OperationFailedException;
import com.simon.http.model.DoResponse;
import com.simon.http.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simon.http.model.PResponse;
import com.simon.system.model.Role;
import com.simon.system.service.RoleService;
import com.simon.token.TokenValidate;
import com.simon.utils.SystemCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName RoleController
 * @Description 角色控制层
 * @Author Jiajie.Mao
 * @Date 2018/6/5 9:38
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/role")
@Api(value = "role", tags = {"角色接口"})
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "添加角色", notes = "根据Role对象创建角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "role", value = "角色对象", required = true, dataType = "Role")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addRole(@RequestBody Role role) {
        Role dRole = roleService.getRoleByName(role.getName());
        if (dRole != null) {
            throw new OperationFailedException("该角色已存在");
        } else {
            //插入数据库
            int res = roleService.addRole(role);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }
        }
    }


    @ApiOperation(value = "查询角色", notes = "根据id获取角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> getRoleById(@RequestParam int id) {
        Role role = roleService.getRoleById(id);
        if (role != null) {
            return new DoResponse<>(role);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取角色列表", notes = "")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> findAllUser() {
        List<Role> roles = roleService.findAllRoles();
        if (roles.size() > 0) {
            return new DoResponse<>(roles);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "查询角色列表", notes = "根据自定义条件分页查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "角色名", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "pageNum", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int")
    })
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @TokenValidate
    public PageResponse<?> queryRoles(@RequestParam String name, @RequestParam int pageNum, @RequestParam int pageSize) {
        List<Role> roles = roleService.queryRoles(name, pageNum, pageSize);
        PResponse bPage = roleService.calPage(name, pageSize);
        if (roles.size() > 0) {
            return new PageResponse<>(roles, roles.size(), bPage.getTotalElements(), bPage.getTotalPage());
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "删除角色", notes = "根据用户id删除角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteRole(@RequestParam int id) {
        int res = roleService.deleteRole(id);
        if (res > 0) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "更新角色", notes = "更新角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "role", value = "角色对象", required = true, dataType = "Role")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateRole(@RequestParam int id, @RequestBody Role role) {
        Role dRole = roleService.getRoleById(id);
        if (dRole != null) {
            int res = roleService.updateRole(dRole, role);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }

        } else {
            throw new OperationFailedException("不存在此数据");
        }
    }


    @ApiOperation(value = "设置权限", notes = "根据角色设置权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "menuids", value = "权限/菜单ids", required = true, allowMultiple = true, dataTypeClass = Integer[].class)
    })
    @RequestMapping(value = "/setAuthority", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> setAuthority(@RequestParam int id, @RequestParam Integer[] menuids) {
        int res = roleService.setAuthority(id, menuids);
        if (res > 0) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "设置用户", notes = "根据角色设置用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userids", value = "用户ids", required = true, allowMultiple = true, dataTypeClass = Integer[].class)
    })
    @RequestMapping(value = "/setUser", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> setUser(@RequestParam int id, @RequestParam Integer[] userids) {
        int res = roleService.setUser(id, userids);
        if (res > 0) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "查询子角色", notes = "根据pid获取子角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "pid", value = "父角色id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/sublist", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> subRoleList(@RequestParam int pid) {
        List<Role> roles = roleService.sublist(pid);
        if (roles != null) {
            return new DoResponse<>(roles);
        } else {
            throw new OperationFailedException();
        }
    }

}
